<template>
  <div class="create">
    <h2 class="margin-bom title">{{title}}</h2>
    <div class="manageFrom">
      <!-- 表单校验 -->
      <el-form :model="news" :rules="rules" ref="news" status-icon>
        <el-form-item label="题目">
          <el-input type="text" v-model="news.tit" :disabled="type == '2'"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" :rows="2" v-model="news.con" :disabled="type == '2'"></el-input>
        </el-form-item>
        <el-form-item label="时间" prop="time">
          <el-date-picker v-model="news.time" value-format="yyyy-MM-dd"  placeholder="选择日期" :disabled="type == '2'"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="createNews()" v-if="id == ''">添加</el-button>
          <!-- 用户不允许修改 -->
          <span v-if="type != '2'">
            <el-button type="success" @click="updateNews()" v-if="id != ''">修改</el-button>
          </span>
          <el-button @click="back()">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import ajax from "../../common/js/ajax";
import API from "../../common/js/api";
import rules from "../../common/js/newsRules";
export default {
  data() {
    return {
      title: "",
      id: "",
      news: {
        tit: "",
        con: "",
        time: ""
      },
      // 权限
      type: "",
      rules: rules()
    };
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    createNews() {
      ajax(
        API.addNews,
        "get",
        this.news,
        d => {
          this.$message({
            type: "success",
            message: "新建成功"
          });
          this.$router.replace("/index/news");
        },
        err => {
          this.$message({
            type: "warning",
            message: err.data.info
          });
        }
      );
    },
    updateNews() {
      delete this.news._id;
      this.news.id = this.id;
      ajax(
        API.updateNews,
        "get",
        this.news,
        d => {
          this.$message({
            type: "success",
            message: "修改成功"
          });
          this.$router.replace("/index/news");
        },
        err => {
          this.$message({
            type: "warning",
            message: err.data.info
          });
        }
      );
    }
  },
  mounted() {
    this.type = JSON.parse(localStorage.getItem("user")).type;
    let len = Object.keys(this.$route.query);
    if (len.length == 0) {
      this.title = "新建通知";
    } else {
      this.title = "编辑通知";
      this.id = this.$route.query.id;
      ajax(
        API.findNews,
        "get",
        { id: this.id },
        d => {
          this.news = d.data.data[0];
        },
        err => {
          alert(err.data.info);
        }
      );
      // 用户
    if(this.type == 2){
      this.title = "查看通知"
    }
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '../../common/css/index.styl';

.el-col {
  line-height: 40px;
}

.create >>> .el-row {
  min-width: 700px;
}

.create >>> .el-input {
  float: right;
  width: 89%;
}

.create >>> .el-textarea {
  float: right;
  width: 89%;
}

.create >>> .el-form-item__error {
  left: 100px;
}

.el-date-editor--date {
  margin-left: 45px !important;
  float: left !important;
  width: 30% !important;
}

.manageFrom {
  width: 60%;
  margin: 0 auto;
  min-width: 700px;
}
</style>